package com.sun.huazhi.aitaskservice.controller;

import com.sun.huazhi.aitaskservice.DTO.response.BasicResponse;
import com.sun.huazhi.aitaskservice.DTO.response.SystemStatusResponse;
import com.sun.huazhi.aitaskservice.common.config.PythonApiProperties;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import io.swagger.v3.oas.annotations.tags.Tag;

@RestController
@Tag(name = "系统状态", description = "提供系统健康检查和状态监控功能")
@RequestMapping("/api/system")
public class SystemController {

    @Autowired
    @Qualifier("pythonApiProperties")
    private PythonApiProperties apiProps;

    @Autowired
    private WebClient webClient;

    @GetMapping("/")
    @Operation(summary = "基础健康检查",
            description = "检查API服务是否正常运行")
    public Mono<ResponseEntity<BasicResponse>> rootStatus() {
        return webClient.get()
                .uri(apiProps.getUrl() + "/")
                .retrieve()
                .bodyToMono(String.class)
                .map(message -> {
                    BasicResponse response = new BasicResponse();
                    response.setMessage(message);
                    return ResponseEntity.ok(response);
                });
    }

    @GetMapping("/system-status")
    @Operation(summary = "系统状态检查",
            description = "获取系统各模块的运行状态和配置信息")
    public Mono<ResponseEntity<SystemStatusResponse>> systemStatus() {
        return webClient.get()
                .uri(apiProps.getUrl() + "/system-status")
                .retrieve()
                .bodyToMono(SystemStatusResponse.class)
                .map(ResponseEntity::ok);
    }

    @GetMapping("/baidu-status")
    @Operation(summary = "百度服务状态",
            description = "检查百度语音服务的详细配置和状态")
    public Mono<ResponseEntity<SystemStatusResponse.SpeechModule>> baiduStatus() {
        return webClient.get()
                .uri(apiProps.getUrl() + "/baidu-status")
                .retrieve()
                .bodyToMono(SystemStatusResponse.SpeechModule.class)
                .map(ResponseEntity::ok);
    }
}
